#!/usr/bin/perl
#
# - Faz Get no apache que utiliza um php para verificar no mainserver do lsf
# - Verifica quantos jogadores online existem nos servidores de LSF UOL
# - Gera saida do Fifo para grafico e no console para pagina dos servidores
#
#  S@ndro - 05/04/2006
#
#	/export/logs/LiveForSpeey/lfs.log
#	Query_salas le este log e atualiza pagina de servidores

my $pipe = "/export/scripts/metricas/getLFSstatus.fifo";

unless ( -p $pipe )
{
   unlink $pipe ;
   system ( 'mkfifo', $pipe ) && die "Can't create pipe $pipe : $!\n";
}

my $check =`fuser $pipe | cut -f2 -d" "`;
chomp $check;

if ($check eq "0")
{
        print "ERROR: Syslog isnt running or listening the fifo file!\n";
        exit 1;
}



use strict;
use Sys::Hostname;
use Socket;
use Fcntl qw(:flock);

	my $cont1="0";
	my $cont2="0"; 
	my $cont3="0"; 
        my $server="zadmin2.adm.intranet";
        my $port = "80";
        my $inf="/LFSStat.php";
        my $protv="HTTP/1.1";
        my $iaddr= inet_aton('zadmin2.adm.intranet') or die "Error: $!";
        my $paddr=sockaddr_in($port, $iaddr) or die "Error: $!";
        my $proto=getprotobyname('tcp') or die "Error - Protocol: $!";

        socket(SOCK1, PF_INET, SOCK_STREAM, $proto) or die "Error: $!";
        connect(SOCK1, $paddr) or die "Error: $!";
        my $msg = "GET $inf $protv\nhost:$server\n\n";
        send(SOCK1, $msg, 0);
	my @out = <SOCK1>;
        close(SOCK1) or die "close $!";
	chomp @out;

foreach (@out)
{
	if ( $_ =~ /UOL LFSBR s2/ )
	{
		$cont1++;
	} 
	if ( $_ =~ /UOL-2 LFSBR s2/ )
	{
	        $cont2++;	
	}
	if ( $_ =~ /UOL#3/ )
	{
	        $cont3++;	
	}
}

# PRINT NO PIPE
open (PIPE, "> $pipe");
my $localtime = time();
print PIPE "LFS_jogadores: LFSUOL1 $cont1 LFSUOL2 $cont2 LFSUOL3 $cont3\n";
close PIPE;

#PRINT NO LOG PARA QUERY_SALAS ATUALIZAR PAGINA DOS SERVIDORES
open (LOG, ">> /export/logs/LiveForSpeey/lfs.log");
print LOG "LFS_jogadores: LFSUOL1 $cont1 LFSUOL2 $cont2 LFSUOL3 $cont3\n";
close LOG;

#PRINT NA TELA PARA DEBUG
print "LFS_jogadores: LFSUOL1 $cont1 LFSUOL2 $cont2 LFSUOL3 $cont3\n";
